﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace DataIO
{
    public partial class frmDataIO : Form
    {
        public frmDataIO()
        {
            InitializeComponent();
            InitUI();
            tm.Interval = 1000;
            tm.Tick += new EventHandler(tm_Tick);
            tm.Start();
        }

        // global var for serial communication  
        public const int maxReadTimeout = 1000;     // max time out for serial read: 1000 ms
        SerialPort sp = new SerialPort();
        string sInput;
        string sStart = "$PCI,";
        char sSpacing = ',';
        string sKP;
        string sEasting;
        string sNorthing;
        string sCP;

        // timer for update text boxes
        System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer();

        private void tm_Tick(object sender, EventArgs e)
        {
            // display data on text boxes
            txtKP.Text = sKP;
            txtEasting.Text = sEasting;
            txtNorthing.Text = sNorthing;
            txtCP.Text = sCP;
        }

        private void InitUI()
        {
            // disable stop button
            btnStop.Enabled = false;

            // add avaiable ports to COM port combo box
            foreach (string str in SerialPort.GetPortNames())
            {
                cbbCOMPort.Items.Add(str);
            }
            cbbCOMPort.Text = "COM2";

        }

        #region Read survey and ROV data from serial ports

        private delegate void GetSerialString(string str);

        private void ReadSerialData()
        {
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.ReadTimeout = maxReadTimeout;
            if (!sp.IsOpen)
            {
                try
                {
                    sp.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            if (sp.IsOpen)
            {
                try
                {
                    string receivedString = sp.ReadLine();
                    this.BeginInvoke(new GetSerialString(SetReceivedString), new object[] { receivedString });
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
        }

        private void SetReceivedString(string receivedString)
        {
            sInput = receivedString;
            sInput = sInput.Remove(0, sStart.Length);

            // using Split method to split string
            string[] words = sInput.Split(sSpacing);
            sKP = words[0];
            sEasting = words[1];
            sNorthing = words[2];
            sCP = words[3];

            // display data on text boxes
            //txtKP.Text = sKP;
            //txtEasting.Text = sEasting;
            //txtNorthing.Text = sNorthing;
            //txtCP.Text = sCP;

            // logging data
            // need code: using access database
        }

        #endregion

        private void btnStop_Click(object sender, EventArgs e)
        {
            // enable read button
            btnRead.Enabled = true;
            // disable stop button
            btnStop.Enabled = false;
            // close serial port
            if (sp.IsOpen)
            {
                try
                {
                    sp.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            sp.PortName = cbbCOMPort.Text;

            // disable read button
            btnRead.Enabled = false;
            // enable stop button
            btnStop.Enabled = true;
            // read serial data from COM port
            ReadSerialData();
        }
    }
}
